#!/usr/bin/env python

# Number for the SRC (Student Research Competition) specialty
SRC = '158'

CODE = '''
//
// Menus for:
//	Submissions
//	Reviewers
//	Topics
//

var Submissions = [
    %s
];

var Topics = [
    %s
];

var Reviewers = [
    %s
];

var vSubmissions = new Array();
var vTopics = new Array();
var vReviewers = new Array();

function
collectObjects(obj)
{
    if (obj.className == "submenu")
	vSubmissions.push(obj)
    else if (obj.className == "topmenu")
	vTopics.push(obj)
    else if (obj.className == "revmenu")
	vReviewers.push(obj)

    for (var i = 0; i < obj.childNodes.length; i++)
	collectObjects(obj.childNodes[i]);
}

function
makeMenu(select, options)
{
    var choice = select.id;
    select.options.length = 0;
    for (var i = 0; i < options.length; i += 2)
    {
	select.options[select.options.length] =
		new Option(options[i+1], options[i]);
	if (options[i] == choice)
	    select.selectedIndex = select.options.length-1;
    }
}

function
sisMenus(root)
{
    vSubmissions.length = 0;
    vTopics.length = 0;
    vReviewers.length = 0;
    collectObjects(root);
    for (var i = 0; i < vSubmissions.length; i++)
	makeMenu(vSubmissions[i], Submissions);
    for (var i = 0; i < vTopics.length; i++)
	makeMenu(vTopics[i], Topics);
    for (var i = 0; i < vReviewers.length; i++)
	makeMenu(vReviewers[i], Reviewers);
}'''

def makeLine(value, label):
    return "    '%s', '%s'," % (value, label)

def makeMenu(filename, select_entry, extra=[]):
    fp = open(filename, 'r')
    menu = extra[:]
    for line in fp.readlines():
        args = line.split()
        if len(args) > 1 and args[0][0] != '#':
            menu.append((' '.join(args[1:]), args[0]))
    menu.sort()
    lines = [ makeLine('', select_entry) ]
    for i in menu:
        lines.append(makeLine(i[1], i[0]))
    return '\n'.join(lines)

subs = makeMenu('map-submissions.txt', 'No submission')
tops = makeMenu('map-topics.txt', 'No topic')
revs = makeMenu('map-reviewers.txt', 'No reviewer')

fp = open('js/sismenus.js', 'w')
fp.write('// Generated by sismenus.py\n')
fp.write(CODE % ( subs, tops, revs ))
fp.write('\n');
